package cn.kooki.app.duobao.util;

import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;

import java.util.ArrayList;

import cn.kooki.app.duobao.data.Bean.User.ContactInfo;
import cn.kooki.app.duobao.util.manager.ContactUtils;

/**
 * Created by xiong on 2016/1/7.
 */
public class ContactHandler extends AsyncQueryHandler {


    private ContactUtils.ContactCallback callback;

    public ContactHandler(ContentResolver cr) {
        super(cr);
    }

    public ContactHandler(ContentResolver cr, ContactUtils.ContactCallback callback) {
        super(cr);
        this.callback = callback;
    }

    @Override
    protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
        super.onQueryComplete(token, cookie, cursor);
        String phoneNumber;
        String phoneName;
        ArrayList<ContactInfo> infos = new ArrayList<>();
        try {
            while (cursor.moveToNext()) {
                phoneNumber = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));//电话号码
                phoneName = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));//姓名
                if (phoneNumber.length() >= 11 && phoneNumber.startsWith("1")) {
                    ContactInfo info = new ContactInfo(phoneName, phoneNumber);
                    infos.add(info);
                }
            }
            if (callback != null) {
                callback.onQueryCompelete(infos);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
